﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using SlidingButtons.Attributes;

[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Blue.Left.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Blue.Right.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Blue.Style.css", "text/css")]

[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Purple.Left.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Purple.Right.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Purple.Style.css", "text/css")]

[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Green.Left.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Green.Right.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Green.Style.css", "text/css")]

[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Red.Left.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Red.Right.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Red.Style.css", "text/css")]

[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.FlatGray.Left.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.FlatGray.Right.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.FlatGray.Style.css", "text/css")]

[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Gray.Left.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Gray.Right.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.Gray.Style.css", "text/css")]

[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.RedTexture.Left.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.RedTexture.Right.png", "image/jpeg")]
[assembly: WebResource("SlidingButtons.SlidingButtonImageAdapter.RedTexture.Style.css", "text/css")]
namespace SlidingButtons.SlidingButtonImageAdapter
{
  [CssResource("SlidingButtons.SlidingButtonImageAdapter.Blue.Style.css")]
  [CssResource("SlidingButtons.SlidingButtonImageAdapter.Purple.Style.css")]
  [CssResource("SlidingButtons.SlidingButtonImageAdapter.Green.Style.css")]
  [CssResource("SlidingButtons.SlidingButtonImageAdapter.Red.Style.css")]
  [CssResource("SlidingButtons.SlidingButtonImageAdapter.FlatGray.Style.css")]
  [CssResource("SlidingButtons.SlidingButtonImageAdapter.Gray.Style.css")]
  [CssResource("SlidingButtons.SlidingButtonImageAdapter.RedTexture.Style.css")]
  public class Adapter : ResourceBuilder
  {
    

    #region LeftBackgroundImageUrl

    /// <summary>
    /// Gets the background image for the left side of the button
    /// </summary>
    public String LeftBackgroundImageUrl
    {
      get
      {
        return Page.ClientScript.GetWebResourceUrl(typeof(Adapter), "SlidingButtons.SlidingButtonImageAdapter." + Control.CssClass + ".Left.png");
      }
    }

    #endregion

    #region RightBackgroundImageUrl

    /// <summary>
    /// Gets the background image for the middle of the button
    /// </summary>
    public String RightBackgroundImageUrl
    {
      get
      {
        return Page.ClientScript.GetWebResourceUrl(typeof(Adapter), "SlidingButtons.SlidingButtonImageAdapter." + Control.CssClass + ".Right.png");
      }
    }

    #endregion

    protected override void Render(HtmlTextWriter writer)
    {
      Button button = this.Control;

      if (String.IsNullOrEmpty(button.CssClass))
      {
        //the developer didn't request a style, so there's no need to output a different format
        base.Render(writer);
        return;
      }

      writer.WriteBeginTag("button"); // <button 

      writer.WriteAttribute("value", "submit"); // <button value="submit" 
      writer.WriteAttribute("id", button.ClientID); // <button value="submit" id="btnButton"
      if (Control.CssClass.Length > 0)
        writer.WriteAttribute("style", "background-image:url('" + this.RightBackgroundImageUrl + "');");

      PostBackOptions pbOptions = new PostBackOptions(button);

      //Add the Postback event
      if (this.Page.ClientScript.GetPostBackEventReference(pbOptions).Length > 0)
      {
        //Turn on Validation if it needs to be on...
        if (button.CausesValidation)
        {
          pbOptions.PerformValidation = true;
          pbOptions.ValidationGroup = button.ValidationGroup;
        }

        if (!String.IsNullOrEmpty(button.PostBackUrl))
        {
          pbOptions.ActionUrl = this.Page.ResolveUrl(button.PostBackUrl);
        }


        //Need to replace double quotes with single quotes for javascript to work.
        writer.WriteAttribute("onclick", this.Control.OnClientClick + this.Page.ClientScript.GetPostBackEventReference(pbOptions).Replace("\"", "'")); // <button value="submit" 

        //Register the Event
        this.Page.ClientScript.RegisterForEventValidation(pbOptions);
      } // <button value="submit" id="btnButton" onclick="__doPostBack('btnButton','')"

      if (button.CssClass.Length > 0)
        writer.WriteAttribute("class", button.CssClass); // <button value="submit" id="btnButton" onclick="__doPostBack('btnButton','')" class="large-button"

      writer.Write(HtmlTextWriter.TagRightChar); // <button value="submit" id="btnButton" onclick="__doPostBack('btnButton','')" class="large-button">

      writer.WriteBeginTag("span");
      if (Control.CssClass.Length > 0)
        writer.WriteAttribute("style", "background-image:url('" + this.LeftBackgroundImageUrl + "');");
      writer.Write(HtmlTextWriter.TagRightChar);
      writer.Write(button.Text);
      writer.WriteEndTag("span"); // <button value="submit" id="btnButton" onclick="__doPostBack('btnButton','')" class="large-button"><span>testButton</span>
      writer.WriteEndTag("button"); // <button value="submit" id="btnButton" onclick="__doPostBack('btnButton','')" class="large-button"><span>testButton</span></button>
    }
  }
}

